﻿
using System;

using OpenTK.Graphics;

namespace Rekx.Graphics
{
    /// <summary>
    /// A base class for OpenGL Buffer objects
    /// </summary>
    public abstract class BufferObject : IDisposable
    {
        int id;
        BufferTarget bufferTarget;

        /// <summary>
        /// Creates a new buffer object of type target
        /// </summary>
        /// <param name="target">Type of the buffer object</param>
        public BufferObject(BufferTarget target)
        {
            bufferTarget = target;
            GL.GenBuffers(1, out id);
        }

        /// <summary>
        /// Write data to the vram stored buffer. 
        /// Insert binds and unbinds from the opengl state, so no extra calls are needed.
        /// </summary>
        /// <param name="size">Size of the data written to vram</param>
        /// <param name="data">The data to write to vram</param>
        public void Insert(int size, object data)
        {
            Bind();
            GL.BufferData(bufferTarget, (IntPtr)size, data, BufferUsageHint.StaticDraw);
            UnBind();
        }

        /// <summary>
        /// Binds the buffer object to the current opengl state. Used for drawing etc.
        /// </summary>
        public void Bind()
        {
            GL.BindBuffer(bufferTarget, id);
        }

        /// <summary>
        /// Unbinds the buffer object from the current opengl state.
        /// </summary>
        public void UnBind()
        {
            GL.BindBuffer(bufferTarget, 0);
        }

        #region IDisposable Pattern
        // taken from msdn

        private bool disposed = false;

        // Implement IDisposable.
        // Do not make this method virtual.
        // A derived class should not be able to override this method.
        /// <summary>
        /// Deletes memory from vram.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            // This object will be cleaned up by the Dispose method.
            // Therefore, you should call GC.SupressFinalize to
            // take this object off the finalization queue
            // and prevent finalization code for this object
            // from executing a second time.
            GC.SuppressFinalize(this);
        }

        // Dispose(bool disposing) executes in two distinct scenarios.
        // If disposing equals true, the method has been called directly
        // or indirectly by a user's code. Managed and unmanaged resources
        // can be disposed.
        // If disposing equals false, the method has been called by the
        // runtime from inside the finalizer and you should not reference
        // other objects. Only unmanaged resources can be disposed.
        private void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    // Dispose managed resources.

                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                // If disposing is false,
                // only the following code is executed.
                GL.DeleteBuffers(1, ref id);

                // Note disposing has been done.
                disposed = true;

            }
        }

        #endregion
    }
}
